package com.example.nonacademic_education.service.impl;

import com.example.nonacademic_education.dao.AdminAuthorityDao;
import com.example.nonacademic_education.dao.AdminDao;
import com.example.nonacademic_education.dao.AuthorityDao;
import com.example.nonacademic_education.data.request.GrantR;
import com.example.nonacademic_education.data.response.AuthorityDto;
import com.example.nonacademic_education.service.AuthorityService;
import com.example.nonacademic_education.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AuthorityServiceImpl implements AuthorityService {
    @Resource
    private AuthorityDao authorityDao;
    @Resource
    private AdminAuthorityDao adminAuthorityDao;
    @Resource
    private AdminDao adminDao;
    @Override
    public R getAll() {
        Map<String, Object> data = new HashMap<>();
        List<AuthorityDto> list=authorityDao.getAll(0L);
        for (AuthorityDto authorityDto : list) {
            authorityDto.setChildren(authorityDao.getAll(authorityDto.getId()));
        }
        data.put("list",list);
        return R.ok(data, "getAll");
    }

    @Override
    public R updateGrant(GrantR grantR) {
        //estimate
        if (grantR.getAdminId()==1L){
            return R.error("该管理员默认拥有全部权限，不能进行修改");
        }
        //find
        List<Long> old=adminAuthorityDao.findByAdminId(grantR.getAdminId());
        //insert
        List<Long> needAdd=grantR.getAuthorityIds().stream()
                .filter(n -> !old.contains(n))
                .collect(Collectors.toList());
        if (needAdd.size()>0) {
            adminAuthorityDao.addAuthority(grantR.getAdminId(),needAdd);
        }
        //delete
        List<Long> needDelete=old.stream()
                .filter(n -> !grantR.getAuthorityIds().contains(n))
                .collect(Collectors.toList());
        if (needDelete.size()>0) {
            adminAuthorityDao.deleteAuthority(grantR.getAdminId(),needDelete);
        }
        return R.ok("更新授权成功");
    }

    @Override
    public R getOneAuthority(Long id) {
        //find
//        String authoritiesString=adminDao.getAuthorities(id);
//        List<Long> authorities=Arrays.stream(authoritiesString.split(","))
//                .map(Long::valueOf)
//                .collect(Collectors.toList());
        List<Long> authorities=adminAuthorityDao.findByAdminId(id);
        Map<String, Object> data = new HashMap<>();
        List<AuthorityDto> list=authorityDao.getAll(0L);
        for (AuthorityDto authorityDto : list) {
            authorityDto.setChildren(authorityDao.getAll(authorityDto.getId()));
            for (int j = 0; j < authorityDto.getChildren().size(); j++) {
                if (authorities.contains(authorityDto.getChildren().get(j).getId())){
                    authorityDto.getChildren().get(j).setIsGranted(1);
                }else {
                    authorityDto.getChildren().get(j).setIsGranted(0);
                }
            }
        }
        data.put("list",list);
        return R.ok(data, "getAll");
    }
}
